WPF实现窗体亚克力效果的示例代码 |
您所在的位置:网站首页 › wpf form表单 › WPF实现窗体亚克力效果的示例代码 |
WPF 窗体设置亚克力效果 框架使用大于等于.NET40。 Visual Studio 2022。 项目使用 MIT 开源许可协议。 WindowAcrylicBlur 设置亚克力颜色。 Opacity 设置透明度。 实现代码 1) 准备WindowAcrylicBlur.cs如下: using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media; using Microsoft.Win32; using Microsoft.Windows.Shell; namespace WPFDevelopers.Controls { internal enum AccentState { ACCENT_DISABLED = 0, ACCENT_ENABLE_GRADIENT = 1, ACCENT_ENABLE_TRANSPARENTGRADIENT = 2, ACCENT_ENABLE_BLURBEHIND = 3, ACCENT_ENABLE_ACRYLICBLURBEHIND = 4, ACCENT_INVALID_STATE = 5 } [StructLayout(LayoutKind.Sequential)] internal struct AccentPolicy { public AccentState AccentState; public uint AccentFlags; public uint GradientColor; public uint AnimationId; } [StructLayout(LayoutKind.Sequential)] internal struct WindowCompositionAttributeData { public WindowCompositionAttribute Attribute; public IntPtr Data; public int SizeOfData; } internal enum WindowCompositionAttribute { // ... WCA_ACCENT_POLICY = 19 // ... } internal class WindowOldConfig { public bool AllowsTransparency; public Brush Background; public WindowChrome WindowChrome; public WindowStyle WindowStyle = WindowStyle.SingleBorderWindow; } internal class WindowOSHelper { public static Version GetWindowOSVersion() { var regKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion"); int major; int minor; int build; int revision; try { var str = regKey.GetValue("CurrentMajorVersionNumber")?.ToString(); int.TryParse(str, out major); str = regKey.GetValue("CurrentMinorVersionNumber")?.ToString(); int.TryParse(str, out minor); str = regKey.GetValue("CurrentBuildNumber")?.ToString(); int.TryParse(str, out build); str = regKey.GetValue("BaseBuildRevisionNumber")?.ToString(); int.TryParse(str, out revision); return new Version(major, minor, build, revision); } catch (Exception) { return new Version(0, 0, 0, 0); } finally { regKey.Close(); } } } public class WindowAcrylicBlur : Freezable { private static readonly Color _BackgtoundColor = Color.FromArgb(0x01, 0, 0, 0); //设置透明色 防止穿透 [DllImport("user32.dll")] internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); private static bool EnableAcrylicBlur(Window window, Color color, double opacity, bool enable) { if (window == null) return false; AccentState accentState; var vOsVersion = WindowOSHelper.GetWindowOSVersion(); if (vOsVersion > new Version(10, 0, 17763)) //1809 accentState = enable ? AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND : AccentState.ACCENT_DISABLED; else if (vOsVersion > new Version(10, 0)) accentState = enable ? AccentState.ACCENT_ENABLE_BLURBEHIND : AccentState.ACCENT_DISABLED; else accentState = AccentState.ACCENT_DISABLED; if (opacity > 1) opacity = 1; var windowHelper = new WindowInteropHelper(window); var accent = new AccentPolicy(); var opacityIn = (uint) (255 * opacity); accent.AccentState = accentState; if (enable) { var blurColor = (uint) ((color.R |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |